package cloud.demarcia.crawler3.utils;


import cloud.demarcia.crawler3.entities.InitInfo;
import cloud.demarcia.crawler3.entities.Page;
import cloud.demarcia.crawler3.processors.Processor;

import com.gargoylesoftware.htmlunit.html.HtmlPage;
import lombok.ToString;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;


@ToString
@Service
public class PageGetter {
    Queue<Page> pageQueue;

    private final InitInfo info;

    @Autowired
    public PageGetter(InitInfo info) {
        this.info = info;
        pageQueue=new LinkedList<>();
    }

    public void addPage(Page page){
        pageQueue.add(page);
    }

    public void addPages(List<Page> list){
        pageQueue.addAll(list);
    }


    public void runQueue() throws Exception{
        long time=System.currentTimeMillis();
        int count=0;
        System.out.println("[CRAWLER] Starting process web pages.");
        while(!pageQueue.isEmpty()){
            Page page= pageQueue.poll();
            count++;
            String beanName =page.getProcessor()+"Processor";
            Processor processor= (Processor)ApplicationUtil.getBean(beanName);
            Document doc=browse(page);
            List<Page> resList=processor.process(doc);
            pageQueue.addAll(resList);
        }
        time=System.currentTimeMillis()-time;
        time/=1000;
        System.out.println("[CRAWLER] Finished process "+count+" pages in "+time+" seconds.");
    }

    private Document browse(Page page) throws IOException, InterruptedException {
        System.out.println("[CRAWLER] Build-in browser is getting page "+page.getUrl());
        HtmlPage htmlPage=Browser.browse(page.getUrl());
        return Jsoup.parse(htmlPage.asXml());
    }

}
